package com.greate.community.controller.interceptor;

import com.greate.community.service.master.SectionForbidService;
import com.greate.community.util.RedisKeyUtil;
import org.apache.http.HeaderIterator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Component
public class SectionForbidInterceptor implements HandlerInterceptor {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    SectionForbidService sectionForbidService;

    /**
     * 全分区禁言  禁言不能发评论
     * @param request
     * @param response
     * @param handler
     * @return
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        //全区禁言
        boolean forbidAll = allSectionForbid(request, response);
        if (forbidAll ==false){
            return false;
        }
        //某些分区禁言
        return someSectionForbid(request, response);

    }



    public boolean allSectionForbid(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String status = (String) redisTemplate.opsForValue().get(RedisKeyUtil.getForbidAllSectionForbid());
        System.out.println(status);
        //处于全分区禁言中
        if (status!=null&& !status.equals("")){

            String requestURI = request.getRequestURI();
            //这些网址无权进入
            if (requestURI.contains("/comment/add")){
                String servletPath = request.getServletPath();
                System.out.println(servletPath.substring(servletPath.lastIndexOf("/")+1));

                response.setContentType("text/html;charset=utf-8");
                response.getWriter().print("<script>alert('全区禁言中 等待系统维护'); window.location.href='http://localhost:8080/index';</script>");
                return  false;
            }
        }
        return true;
    }
    private boolean someSectionForbid(HttpServletRequest request, HttpServletResponse response) throws IOException {


        Set<List<Integer>> members = redisTemplate.opsForSet().members(RedisKeyUtil.getForbidSomeSectionForbid());

        Iterator<List<Integer>> iterator = members.iterator();
        List<Integer> sections = new ArrayList<>();
        while (iterator.hasNext()){
            sections=iterator.next();
        }

// 处于分区禁言中
        if (sections!=null&& sections.size()>0){
            String requestURI = request.getRequestURI();
            //处于发帖状态时
            if (requestURI.contains("/comment/add")){
                //判断当前帖子所属分区
                String servletPath = request.getServletPath();
                String discussId = servletPath.substring(servletPath.lastIndexOf("/")+1);
                Integer i = sectionForbidService.discussOfSection(Integer.parseInt(discussId));
                //不确定有用
                for (int j=0;j<sections.size();j++){

                    Integer s = sections.get(j);
                    if (i.equals(s)){
                        response.setContentType("text/html;charset=utf-8");
                        response.getWriter().print("<script>alert('该分区禁言中 等待系统维护'); window.location.href='http://localhost:8080/index';</script>");
                        return  false;
                    }
                }
            }
        }
        return true;
    }
}
